package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ClientBean;
import com.project.mapper.IClientMapper;
import com.project.service.IClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ClientServiceImpl implements IClientService {
    @Autowired
    private IClientMapper clientMapper;
    @Override
    public IPage<ClientBean> findByItem(Integer pageNO, String name, String ranges) {
        QueryWrapper qw=new QueryWrapper<>();
        if (name!=null && !name.equals("")){
            qw.like("c_name",name);
        }
        if (ranges!=null && !ranges.equals("")){
            qw.like("c_ranges",ranges);
        }
        return clientMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public ClientBean findById(Integer clientId) {
        return clientMapper.selectById(clientId);
    }

    @Override
    public String addClient(ClientBean client) {
        QueryWrapper qw=new QueryWrapper<>();
        qw.eq("c_name",client.getName());
        if (clientMapper.selectOne(qw)==null){
            clientMapper.insert(client);
            return "ok";
        }
        return "";
    }

    @Override
    public void updateClient(Integer clientId, String headerName, String headerPhone, String range) {
        ClientBean clientBean=clientMapper.selectById(clientId);
        clientBean.setHeaderName(headerName);
        clientBean.setHeaderPhone(headerPhone);
        clientBean.setRanges(range);
        clientMapper.updateById(clientBean);
    }

    @Override
    public List<ClientBean> findAll() {
        return clientMapper.selectList(null);
    }

    @Override
    public ClientBean findByName(String name) {
        QueryWrapper<ClientBean> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("c_name", name);
        return clientMapper.selectOne(queryWrapper);
    }
}
